package com.woniu.project.core.utils;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import javax.crypto.SecretKey;
import java.util.Date;
import java.util.Map;

/**
 * @Author: Klaus
 * @Date: 2020/9/24 11:42
 */
public class JwtUtil {

    private static String keyWord = "asadg123gjhdsjhfjhj123jhj4h35l6kl4j5k7654lkj345j34kl5jk1l214jhj346gjg73kjhhjhj324234hj2g5jg6j";

    //创建一个Token
    public static String creatToken(Map<String,Object> maps,Integer expTimeSec){
        SecretKey secretKey = Keys.hmacShaKeyFor(keyWord.getBytes());

        return Jwts.builder()
                .setClaims(maps)
                .signWith(secretKey)
                .setExpiration(new Date(expTimeSec * 1000 + System.currentTimeMillis()))
                .compact();
    }

    public static Map<String, Object> parseToken(String jwt){
        SecretKey secretKey = Keys.hmacShaKeyFor(keyWord.getBytes());
        return Jwts.parserBuilder()
                .setSigningKey(secretKey)
                .build()
                .parseClaimsJws(jwt)
                .getBody();
    }

    public static void main(String[] args) {
        Map<String, Object> map = JwtUtil.parseToken("eyJhbGciOiJIUzUxMiJ9.eyJtYW5hZ2VtZW50TmFtZSI6InJvb3QiLCJwZXJtaXNzaW9uVHlwZUlkOjEyIjp7InBlcm1pc3Npb25JZCI6IjQ1IiwicGVybWlzc2lvblR5cGVJZCI6IjEyIiwicGVybWlzc2lvbk5hbWUiOiLmn6Xor6LpibTlrprorrDlvZUiLCJwZXJtaXNzaW9uQWRkciI6Ii9wYXduL2Fzc2Vzcy9pZGVudGl0eS9zZWxlY3RJZGVudGl0eUxpc3QifSwibWFuYWdlbWVudFN0YXR1cyI6Iuato-W4uCIsInBlcm1pc3Npb25UeXBlSWQ6MTEiOnsicGVybWlzc2lvbklkIjoiMzIiLCJwZXJtaXNzaW9uVHlwZUlkIjoiMTEiLCJwZXJtaXNzaW9uTmFtZSI6IuWIoOmZpOmXqOW6lyIsInBlcm1pc3Npb25BZGRyIjoiL3Bhd24vc2hvcC9zaG9wL2RlbGV0ZU9uZVNob3AifSwicGVybWlz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bk5hbWUiOiLlsI_nsbvliKDpmaQiLCJwZXJtaXNzaW9uQWRkciI6Ii9wYXduL3Byb2R1Y3QvcHJvZHVjdFNtYWxsVHlwZS9kZWxldGVCeUlkIn0sInBlcm1pc3Npb25UeXBlSWQ6MTMiOnsicGVybWlzc2lvbklkIjoiNDkiLCJwZXJtaXNzaW9uVHlwZUlkIjoiMTMiLCJwZXJtaXNzaW9uTmFtZSI6IuagueaNruWVhuWTgeWkp-exu2lk5p-l6K-i6Ym05a6a5Zu-5a6a5LmJIiwicGVybWlzc2lvbkFkZHIiOiIvcGF3bi9wcm9kdWN0L3Byb2R1Y3RCaWdUeXBlL3NlbGVjdGlkZW50aWZpY2F0aW9uTWFwIn0sInBlcm1pc3Npb25UeXBlSWQ6MTgiOnsicGVybWlzc2lvbklkIjoiNjYiLCJwZXJtaXNzaW9uVHlwZUlkIjoiMTgiLCJwZXJtaXNzaW9uTmFtZSI6IuWbvueJh-S4iuS8oCIsInBlcm1pc3Npb25BZGRyIjoiL3Bhd24vcHJvZHVjdC91cGxvYWQvaWNvblVwbG9hZCJ9LCJwZXJtaXNzaW9uVHlwZUlkOjE3Ijp7InBlcm1pc3Npb25JZCI6IjY1IiwicGVybWlzc2lvblR5cGVJZCI6IjE3IiwicGVybWlzc2lvbk5hbWUiOiLmoLnmja7llYblk4Hlrp7kvZNpZOafpeivouWVhuWTgeWxnuaApyIsInBlcm1pc3Npb25BZGRyIjoiL3Bhd24vcHJvZHVjdC9wcm9kdWN0UHJvcGVydHkvc2VsZWN0QnlFbnRpdHlJZCJ9LCJleHAiOjE2MDMwNzg5NjJ9.3BzFb8FxlGw8t4ALidzY-pvnzDm1hail3TMimxZdxNTU5hBIcvV83QfBN-KRl3EUMhkuOsbLNr2nxLzaNDw8Vw");
        for (Map.Entry<String, Object> stringObjectEntry : map.entrySet()) {
            String key = stringObjectEntry.getKey();
            Object value = stringObjectEntry.getValue();

            System.out.println("key==="+key+"value==="+value);
        }
    }


}
